

#pip install playwright
#playwright install-deps
#playwright install chromium-headless-shell


def run(params:list[str]):
    from playwright.sync_api import sync_playwright
    await stagehand.init();
    await stagehand.page.goto("https://github.com/browserbase/stagehand");
    await stagehand.act({ action: "click on the contributors" });
    const contributor = await stagehand.extract({
      instruction: "extract the top contributor",
      schema: z.object({
        username: z.string(),
        url: z.string(),
      }),
    });
    await stagehand.close();
    console.log(`Our favorite contributor is ${contributor.username}`);

    # 启动浏览器
    browser = playwright.chromium.launch(headless=True)
    # 创建新页面
    page = browser.new_page()
    # 访问百度
    page.goto("https://www.baidu.com")
    # 在搜索框中输入“小红薯”
    page.fill("input[name='wd']", "python")
    # 触发搜索
    page.keyboard.press("Enter")
    # 等待搜索结果加载
    page.wait_for_load_state("domcontentloaded")

    # 获取搜索结果
    results = page.query_selector_all(".result")
    # 打印前5条搜索结果的内容和URL
    for i, result in enumerate(results[:5]):
        title = result.text_content()
        url = result.get_attribute("href")
        print(f"Result {i+1}:")
        print(f"Title: {title}")
        print(f"URL: {url}")
        print()
    # 关闭浏览器
    browser.close()

    with sync_playwright() as playwright:
        run(playwright)
